import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
var AbstractCache = /*#__PURE__*/function () {
  function AbstractCache(keyPrefix) {
    _classCallCheck(this, AbstractCache);
    /** 特定前缀，作为筛分的命名空间 */
    _defineProperty(this, "keyPrefix", '');
    this.keyPrefix = keyPrefix;
  }
  _createClass(AbstractCache, [{
    key: "getPrefixedKey",
    value: function getPrefixedKey(key) {
      return "".concat(this.keyPrefix, "::").concat(key);
    }
  }]);
  return AbstractCache;
}();
/** 内存缓存 */
var MemoryCache = /*#__PURE__*/function (_AbstractCache) {
  _inherits(MemoryCache, _AbstractCache);
  var _super = _createSuper(MemoryCache);
  function MemoryCache() {
    var _this;
    _classCallCheck(this, MemoryCache);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _super.call.apply(_super, [this].concat(args));
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    _defineProperty(_assertThisInitialized(_this), "data", {});
    return _this;
  }
  _createClass(MemoryCache, [{
    key: "set",
    value:
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    function set(key, value, options) {
      var prefixedKey = this.getPrefixedKey(key);
      this.data[prefixedKey] = {
        value: value,
        expire: options !== null && options !== void 0 && options.expire ? new Date().getTime() + (options === null || options === void 0 ? void 0 : options.expire) : undefined
      };
    }
  }, {
    key: "get",
    value: function get(key) {
      var prefixedKey = this.getPrefixedKey(key);
      var cachedData = this.data[prefixedKey];
      if (cachedData !== null && cachedData !== void 0 && cachedData.expire && (cachedData === null || cachedData === void 0 ? void 0 : cachedData.expire) < new Date().getTime()) {
        return undefined;
      }
      return cachedData === null || cachedData === void 0 ? void 0 : cachedData.value;
    }
  }, {
    key: "clear",
    value: function clear(key) {
      if (key) {
        var prefixedKey = this.getPrefixedKey(key);
        delete this.data[prefixedKey];
        return;
      }
      this.data = {};
    }
  }, {
    key: "length",
    value: function length() {
      return Object.keys(this.data).length;
    }
  }]);
  return MemoryCache;
}(AbstractCache);
var StorageCache = /*#__PURE__*/function (_AbstractCache2) {
  _inherits(StorageCache, _AbstractCache2);
  var _super2 = _createSuper(StorageCache);
  function StorageCache() {
    var _this2;
    _classCallCheck(this, StorageCache);
    for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
      args[_key2] = arguments[_key2];
    }
    _this2 = _super2.call.apply(_super2, [this].concat(args));
    _defineProperty(_assertThisInitialized(_this2), "storage", void 0);
    return _this2;
  }
  _createClass(StorageCache, [{
    key: "set",
    value:
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    function set(key, value, options) {
      var _this$storage;
      var prefixedKey = this.getPrefixedKey(key);
      (_this$storage = this.storage) === null || _this$storage === void 0 || _this$storage.setItem(prefixedKey, JSON.stringify({
        value: value,
        expire: options !== null && options !== void 0 && options.expire ? new Date().getTime() + (options === null || options === void 0 ? void 0 : options.expire) : undefined
      }));
    }
  }, {
    key: "get",
    value: function get(key) {
      var _this$storage2;
      var prefixedKey = this.getPrefixedKey(key);
      var storagedVal = (_this$storage2 = this.storage) === null || _this$storage2 === void 0 ? void 0 : _this$storage2.getItem(prefixedKey);
      if (!storagedVal) return undefined;
      var cachedData = JSON.parse(storagedVal);
      if (cachedData !== null && cachedData !== void 0 && cachedData.expire && (cachedData === null || cachedData === void 0 ? void 0 : cachedData.expire) < new Date().getTime()) {
        return undefined;
      }
      return cachedData.value;
    }
  }, {
    key: "clear",
    value: function clear(key) {
      var _this$storage4;
      if (key) {
        var _this$storage3;
        var prefixedKey = this.getPrefixedKey(key);
        (_this$storage3 = this.storage) === null || _this$storage3 === void 0 || _this$storage3.removeItem(prefixedKey);
        return;
      }
      (_this$storage4 = this.storage) === null || _this$storage4 === void 0 || _this$storage4.clear();
    }
  }, {
    key: "length",
    value: function length() {
      var _this$storage$length, _this$storage5;
      return (_this$storage$length = (_this$storage5 = this.storage) === null || _this$storage5 === void 0 ? void 0 : _this$storage5.length) !== null && _this$storage$length !== void 0 ? _this$storage$length : 0;
    }
  }]);
  return StorageCache;
}(AbstractCache);
/** LocalStorage 缓存 */
var LocalStorageCache = /*#__PURE__*/function (_StorageCache) {
  _inherits(LocalStorageCache, _StorageCache);
  var _super3 = _createSuper(LocalStorageCache);
  function LocalStorageCache() {
    var _this3;
    _classCallCheck(this, LocalStorageCache);
    for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
      args[_key3] = arguments[_key3];
    }
    _this3 = _super3.call.apply(_super3, [this].concat(args));
    _defineProperty(_assertThisInitialized(_this3), "storage", localStorage);
    return _this3;
  }
  return _createClass(LocalStorageCache);
}(StorageCache);
/** SessionStorage 缓存 */
var SessionStorageCache = /*#__PURE__*/function (_StorageCache2) {
  _inherits(SessionStorageCache, _StorageCache2);
  var _super4 = _createSuper(SessionStorageCache);
  function SessionStorageCache() {
    var _this4;
    _classCallCheck(this, SessionStorageCache);
    for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
      args[_key4] = arguments[_key4];
    }
    _this4 = _super4.call.apply(_super4, [this].concat(args));
    _defineProperty(_assertThisInitialized(_this4), "storage", sessionStorage);
    return _this4;
  }
  return _createClass(SessionStorageCache);
}(StorageCache);
/** 创建缓存 */
export var createCache = function createCache(options) {
  var _ref = options !== null && options !== void 0 ? options : {},
    _ref$type = _ref.type,
    type = _ref$type === void 0 ? 'memory' : _ref$type,
    _ref$keyPrefix = _ref.keyPrefix,
    keyPrefix = _ref$keyPrefix === void 0 ? '' : _ref$keyPrefix;
  if (type === 'localStorage') {
    return new LocalStorageCache(keyPrefix);
  } else if (type === 'sessionStorage') {
    return new SessionStorageCache(keyPrefix);
  }
  return new MemoryCache(keyPrefix);
};